package com.dwy2002.pojo.po.task;

import com.dwy2002.pojo.po.base.BaseDeleted;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.time.LocalDateTime;

/**
 * 任务类
 *
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/07/14
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class Task extends BaseDeleted {
    /**
     * 任务名称
     */
    private String title;

    /**
     * 任务详细描述
     */
    private String description;

    /**
     * 优先级：1-紧急重要 2-重要不紧急 3-一般
     */
    private Integer priority;

    /**
     * 任务截止日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8", shape = JsonFormat.Shape.STRING)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime dueDate;

    /**
     * 任务进度状态：1-已发布 2-进行中 3-等待审核 4-已完成 5-已延期 6-已关闭
     */
    private Integer progress;

    /**
     * 责任人ID（关联user表）
     */
    private String assigneeId;

    /**
     * 最后一次提醒时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8", shape = JsonFormat.Shape.STRING)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime lastReminderAt;

    /**
     * 创建人ID
     */
    private String creatorId;

    @Getter
    public enum TaskPriorityEnum {

        URGENT_IMPORTANT(1, "紧急重要"),
        IMPORTANT_NOT_URGENT(2, "重要不紧急"),
        GENERAL(3, "一般");

        private final int code;
        private final String description;

        TaskPriorityEnum(int code, String description) {
            this.code = code;
            this.description = description;
        }

        /**
         * 验证数值是否为有效的优先级
         *
         * @param value 优先级代码
         * @return true=有效，false=无效
         * @author 杜伟毅
         * @since 2025/7/14 0014 16:31
         */
        public static boolean isValidPriority(int value) {
            for (TaskPriorityEnum e : values()) {
                if (e.code == value) {
                    return true;
                }
            }
            return false;
        }

        /**
         * 判断是否为紧急重要优先级
         *
         * @param value 优先级代码
         * @return true=是紧急重要，false=不是
         * @author 杜伟毅
         * @since 2025/7/14 0014 16:31
         */
        public static boolean isUrgentImportant(int value) {
            return value == URGENT_IMPORTANT.code;
        }

        /**
         * 判断是否为重要不紧急优先级
         *
         * @param value 优先级代码
         * @return true=是重要不紧急，false=不是
         * @author 杜伟毅
         * @since 2025/7/14 0014 16:31
         */
        public static boolean isImportantNotUrgent(int value) {
            return value == IMPORTANT_NOT_URGENT.code;
        }

        /**
         * 判断是否为一般优先级
         *
         * @param value 优先级代码
         * @return true=是一般优先级，false=不是
         */
        public static boolean isGeneral(int value) {
            return value == GENERAL.code;
        }

        /**
         * 根据优先级代码获取描述
         *
         * @param code 优先级代码
         * @return 对应的描述文本
         * @throws IllegalArgumentException 如果代码无效
         */
        public static String getDescriptionByCode(int code) {
            for (TaskPriorityEnum e : values()) {
                if (e.code == code) {
                    return e.description;
                }
            }
            throw new IllegalArgumentException("无效的优先级代码: " + code);
        }
    }
}
